﻿using JGSoft.U9CRTAPIEX.DTOs.Enum;
using JGSoft.U9CRTAPIEX.Public;
using Newtonsoft.Json;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using U9C.ExtendAPI.DTOs.RequestDTOs;
using U9C.ExtendAPI.Public;
using UFIDA.U9.Base.Currency;
using UFIDA.U9.Base.FlexField.KeyFlexField;
using UFIDA.U9.Base.Location;
using UFIDA.U9.Base.Organization;
using UFIDA.U9.Base.PropertyTypes;
using UFIDA.U9.Base.UOM;
using UFIDA.U9.CBO.FI.CommonFIHelper;
using UFIDA.U9.CBO.FI.ConfirmTerm;
using UFIDA.U9.CBO.FI.RecPayTerm;
using UFIDA.U9.CBO.FI.Tax;
using UFIDA.U9.CBO.HR.Department;
using UFIDA.U9.CBO.HR.Operator;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.CBO.SCM.ShipmentBy;
using UFIDA.U9.CBO.SCM.Supplier;
using UFIDA.U9.CBO.SCM.Supplier.Proxy;
using UFIDA.U9.ISV.PUB.RestSV.Model.CBO.Supplier;
using UFIDA.U9.SM.SO;
using UFIDA.U9.SM.SOModify;
using UFIDA.U9.SM.SOModify.Proxy;
using UFIDA.U9.SQL.CLRLib;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Business;
using UFSoft.UBF.MVC.Common;
using UFSoft.UBF.Transactions;
using UFSoft.UBF.Util.Context;
using static U9C.ExtendAPI.DTOs.RequestDTOs.OAItemAddPara;

namespace U9C.ExtendAPI.Controllers
{
    public class BaseSyncController : ApiController
    {
        private readonly string _userCode;
        private readonly string _entCode;
        private string _orgCode;
        public BaseSyncController()
        {
            JsonConfig _config = new JsonConfig();
            _userCode = _config.SelectToken("UserCode");
            _entCode = _config.SelectToken("EntCode");
            _orgCode = _config.SelectToken("OrgCode");
        }
        /// <summary>
        /// 客户分类
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> CustomerType(List<CodeNamePara> paras)
        {
            using (BPForEngine bp = new BPForEngine())
            {
                _orgCode = paras[0].OrgCode;
                PublicMethod.WriteContext(_userCode, _entCode, _orgCode, JsonConvert.SerializeObject(paras), this.GetType().Name);
                var Org = Organization.FindByCode(_orgCode);
                var exists = CustomerCategory.Finder.FindAll($"Code in ({PublicMethod.StringJoin(paras.Select(t => t.Code))})").Cast<CustomerCategory>().ToList();
                using (ISession session = Session.Open())
                {
                    foreach (var para in paras)
                    {
                        var category = exists.FirstOrDefault(t => t.Code == para.Code);
                        if (category == null)
                        {
                            category = CustomerCategory.Create();
                            category.Org = Org;
                            category.Code = para.Code;
                            category.Name = para.Name;
                        }
                        else
                        {
                            category.Name = para.Name;
                        }
                    }
                    session.Commit();
                }
                return new ApiResult<string>();
            }
        }
        /// <summary>
        /// 客户档案同步
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> CustomerSave(List<CustomerSavePara> paras)
        {
            using (BPForEngine bp = new BPForEngine())
            {
                _orgCode = paras[0].OrgCode;
                PublicMethod.WriteContext(_userCode, _entCode, _orgCode, JsonConvert.SerializeObject(paras), this.GetType().Name);
                var Org = Organization.FindByCode(_orgCode);
                var categorys = paras.Select(t => t.CategoryCode);
                var customerCategorys = new List<CustomerCategory>();
                if (categorys.Count() > 0)
                    customerCategorys = CustomerCategory.Finder.FindAll($"Code in ({PublicMethod.StringJoin(categorys)})").Cast<CustomerCategory>().ToList();
                var territory = Territory.FindByCode("215");
                var taxSchedule = PublicMethod.GetDistinctElements<TaxSchedule>("Code", paras.Select(t => t.TaxSchedule));
                var shipmentBy = ShipmentBy.FindByCode("001");
                var recervalTerm = RecTerm.Finder.Find($"Code = 'YZ01'");
                //立账条件
                var ARAccrueTerms = PublicMethod.GetDistinctElements<ARAccrueTerm>("Code", paras.Select(t => t.ARConfirmTerm));
                //币种
                var Currencys = PublicMethod.GetDistinctElements<Currency>("Code", paras.Select(t => t.TradeCurrency));
                var departs = paras.Select(t => t.DepartmentName);
                var depart = new List<Department>();
                if (departs.Count() > 0)
                    depart = Department.Finder.FindAll($"Name in ({PublicMethod.StringJoin(departs)})").Cast<Department>().ToList();
                var salesers = paras.Select(t => t.SaleserCode);
                var saleser = new List<Operators>();
                if (salesers.Count() > 0)
                    saleser = Operators.Finder.FindAll($"Code in ({PublicMethod.StringJoin(salesers)})").Cast<Operators>().ToList();
                var exists = Customer.Finder.FindAll($"Code in ({PublicMethod.StringJoin(paras.Select(t => t.Code))})").Cast<Customer>().ToList();
                using (ISession session = Session.Open())
                {
                    foreach (var para in paras)
                    {
                        var customer = exists.FirstOrDefault(t => t.Code == para.Code);
                        if (customer == null)
                        {
                            customer = Customer.Create();
                            customer.OrgKey = new Organization.EntityKey(Org.ID);
                            customer.KeyFlexFieldStru = KeyFlexFieldStru.Finder.FindByID(2);
                            customer.Code = para.Code;
                            customer.Name = para.Name;
                            customer.CustomerCategory = customerCategorys.FirstOrDefault(t => t.Code == para.CategoryCode) ?? throw new Exception($"未找到编码为{para.CategoryCode}的客户分类");
                            customer.Territory = territory;
                            customer.TaxSchedule = taxSchedule.FirstOrDefault(t => t.Code == para.TaxSchedule);// ?? throw new Exception($"未找到编码为{para.TaxCode}的税组合");
                            customer.ShippmentRule = shipmentBy;
                            customer.RecervalTerm = recervalTerm;
                            customer.Department = depart.FirstOrDefault(t => t.Name == para.DepartmentName);
                            customer.Saleser = saleser.FirstOrDefault(t => t.Code == para.SaleserCode);
                            customer.ARConfirmTerm = ARAccrueTerms.FirstOrDefault(t => t.Code == para.ARConfirmTerm);
                            customer.TradeCurrency = Currencys.FirstOrDefault(t => t.Code == para.TradeCurrency);
                            customer.Effective = new UFIDA.U9.Base.PropertyTypes.Effective
                            {
                                IsEffective = true,
                                EffectiveDate = new DateTime(2000, 1, 1),
                                DisableDate = new DateTime(9999, 12, 31)
                            };
                            customer.IsPriceListModify = true;
                            customer.IsShipmentModify = true;
                            customer.IsRecTermModify = true;
                            customer.IsARCfmModify = true;
                            customer.IsCreditCheck = false;

                            CustomerSite customerSite = CustomerSite.Create(customer);
                            customerSite.Code = para.Code;
                            customerSite.Name = para.Name;
                            customerSite.IsShipTo = true;
                            customerSite.IsDefaultShipTo = true;
                            customerSite.IsBillTo = true;
                            customerSite.IsDefaultBillTo = true;
                            customerSite.IsClaim = true;
                            customerSite.IsDefaultClaim = true;
                            customerSite.IsPayment = true;
                            customerSite.IsDefaultPayment = true;
                            customerSite.IsContrast = true;
                            customerSite.IsDefaultContrast = true;
                            customerSite.ShippmentRule = shipmentBy;
                            customerSite.TaxSchedule = taxSchedule.FirstOrDefault(t => t.Code == para.TaxSchedule);
                            customerSite.ARConfirmTerm = ARAccrueTerms.FirstOrDefault(t => t.Code == para.ARConfirmTerm);
                            customerSite.TradeCurrency = Currencys.FirstOrDefault(t => t.Code == para.TradeCurrency);
                            customerSite.PaymentTerm = recervalTerm;

                            customer.CustomerSites.Add(customerSite);
                        }
                        else
                        {
                            customer.Name = para.Name;
                            customer.CustomerCategory = customerCategorys.FirstOrDefault(t => t.Code == para.CategoryCode) ?? throw new Exception($"未找到编码为{para.CategoryCode}的客户分类");
                            customer.Department = depart.FirstOrDefault(t => t.Name == para.DepartmentName);
                            customer.Saleser = saleser.FirstOrDefault(t => t.Code == para.SaleserCode);
                        }
                    }
                    session.Commit();
                }
            }
            return new ApiResult<string>();
        }
        /// <summary>
        /// 供应商分类
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> SupplierType(List<CodeNamePara> paras)
        {
            using (BPForEngine bp = new BPForEngine())
            {
                _orgCode = paras[0].OrgCode;
                PublicMethod.WriteContext(_userCode, _entCode, _orgCode, JsonConvert.SerializeObject(paras), this.GetType().Name);
                var Org = Organization.FindByCode(_orgCode);
                var exists = SupplierCategory.Finder.FindAll($"Code in ({PublicMethod.StringJoin(paras.Select(t => t.Code))})").Cast<SupplierCategory>().ToList();
                using (ISession session = Session.Open())
                {
                    foreach (var para in paras)
                    {
                        var category = exists.FirstOrDefault(t => t.Code == para.Code);
                        if (category == null)
                        {
                            category = SupplierCategory.Create();
                            category.Org = Org;
                            category.Code = para.Code;
                            category.Name = para.Name;
                        }
                        else
                        {
                            category.Name = para.Name;
                        }
                    }
                    session.Commit();
                }
                return new ApiResult<string>();
            }
        }
        /// <summary>
        /// 供应商档案
        /// </summary>
        /// <param name="supplierDTOs"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> SupplierSave(List<NewSupplierDTO> supplierDTOs)
        {
            using (BPForEngine bp = new BPForEngine())
            {
                PublicMethod.WriteContext(_userCode, _entCode, _orgCode, JsonConvert.SerializeObject(supplierDTOs), this.GetType().Name);
                var exists = Supplier.Finder.FindAll($"Code in ({PublicMethod.StringJoin(supplierDTOs.Select(t => t.Code))})").Cast<Supplier>().ToList();
                try
                {
                    var exists1 = supplierDTOs.Where(t => exists.Select(u => u.Code).Contains(t.Code));
                    supplierDTOs = supplierDTOs.Except(exists1).ToList();
                    BatchCreateSupplierByDTOSVProxy batchCreateSupplierByDTOSVProxy = new BatchCreateSupplierByDTOSVProxy();
                    List<SupplierDTOData> list2 = new List<SupplierDTOData>();
                    List<SupplierDTOData> supplierList = GetSupplierList(supplierDTOs, out list2);
                    batchCreateSupplierByDTOSVProxy.SupplierDTOs = supplierList;
                    List<SupplierData> list3 = batchCreateSupplierByDTOSVProxy.Do();
                    if (exists.Count > 0)
                    {
                        foreach (var item in exists1)
                        {
                            var supplier = exists.FirstOrDefault(t => t.Code == item.Code);
                            if (supplier != null)
                            {
                                supplier.Name = item.ShortName;
                            }
                        }
                    }
                    return new ApiResult<string>();
                }
                catch (Exception ex)
                {
                    LogManager.WriteLog($"{ex.Message}\r\n{ex.StackTrace}", "BaseInfoRequest");
                    return new ApiResult<string>
                    {
                        ResCode = 1,
                        ResMsg = ex.Message
                    };
                }
            }
        }
        /// <summary>
        /// 部门
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<string> DepartmentSave()
        {
            using (BPForEngine bp = new BPForEngine())
            {
                PublicMethod.WriteContext(_userCode, _entCode, _orgCode, "", this.GetType().Name);
                HttpHelper httpHelper = new HttpHelper();
                JsonConfig _config = new JsonConfig();
                var OAUrl = _config.SelectToken("OA.URL");
                httpHelper.URL = OAUrl + "/api/hrm/resful/getHrmdepartmentWithPage";
                var postPara = new
                {
                    @params = new
                    {
                        modified = $"{new DateTime(1970, 1, 1):yyyy-MM-dd HH:mm:ss}",
                        pagesize = 100,
                        curpage = 1
                    }
                };
                httpHelper.Content = JsonConvert.SerializeObject(postPara);
                httpHelper.OAGetToken();
                var postResult = httpHelper.Request();

                return new ApiResult<string>()
                {
                    Data = _orgCode
                };
            }
        }
        /// <summary>
        /// 人员
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<string> OperatorSave()
        {
            using (BPForEngine bp = new BPForEngine())
            {
                PublicMethod.WriteContext(_userCode, _entCode, _orgCode, "", this.GetType().Name);
                HttpHelper httpHelper = new HttpHelper();
                JsonConfig _config = new JsonConfig();
                var OAUrl = _config.SelectToken("OA.URL");
                httpHelper.URL = OAUrl + "/api/hrm/resful/getHrmUserInfoWithPage";
                var postPara = new
                {
                    @params = new
                    {
                        modified = $"{new DateTime(2022, 1, 1):yyyy-MM-dd HH:mm:ss}",
                        pagesize = 100,
                        curpage = 1
                    }
                };
                httpHelper.Content = JsonConvert.SerializeObject(postPara);
                httpHelper.OAGetToken();
                var postResult = httpHelper.Request();

                return new ApiResult<string>()
                {
                    Data = postResult
                };
            }
        }
        /// <summary>
        /// 部门
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        //[HttpPost]
        //public ApiResult<string> DepartmentSave(List<CodeNamePara> paras)
        //{
        //    using (BPForEngine bp = new BPForEngine())
        //    {
        //        PublicMethod.WriteContext(_userCode, _entCode, _orgCode, JsonConvert.SerializeObject(paras), this.GetType().Name);
        //        using (ISession session = Session.Open())
        //        {
        //            var org = Organization.FindByCode(_orgCode);
        //            foreach (var para in paras)
        //            {
        //                var depar = Department.Create();
        //                depar.Code = para.Code;
        //                depar.Name = para.Name;
        //                depar.OrgKey = new Organization.EntityKey(org.ID);
        //            }
        //            session.Commit();
        //        }
        //        return new ApiResult<string>();
        //    }
        //}
        ///// <summary>
        ///// 业务员
        ///// </summary>
        ///// <param name="paras"></param>
        ///// <returns></returns>
        ///// <exception cref="Exception"></exception>
        //[HttpPost]
        //public ApiResult<string> OperatorsSave(List<OperatorsSavePara> paras)
        //{
        //    using (BPForEngine bp = new BPForEngine())
        //    {
        //        PublicMethod.WriteContext(_userCode, _entCode, _orgCode, JsonConvert.SerializeObject(paras), this.GetType().Name);
        //        using (ISession session = Session.Open())
        //        {
        //            var org = Organization.FindByCode(_orgCode);
        //            var dept = Department.Finder.FindAll($"Code in ({string.Join(",", paras.Select(t => $"'{t.DeptCode}'"))})").Cast<Department>().ToList();
        //            foreach (var para in paras)
        //            {
        //                var oper = Operators.Create();
        //                oper.Code = para.Code;
        //                oper.Name = para.Name;
        //                oper.Org = org;
        //                oper.Dept = dept.FirstOrDefault(t => t.Code == para.DeptCode) ?? throw new Exception($"未找到编码为{para.DeptCode}的部门，请先创建");
        //            }
        //            session.Commit();
        //        }
        //        return new ApiResult<string>();
        //    }
        //}
        /// <summary>
        /// 料品有效性
        /// </summary>
        /// <param name="itemCodes"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<string> ItemEnable(List<ItemEnablePara> paras)
        {
            using (BPForEngine bp = new BPForEngine())
            {
                if (paras.Count == 0)
                {
                    LogManager.WriteLog(JsonConvert.SerializeObject(paras), GetType().Name);
                    throw new Exception("参数错误:参数需为数组");
                }
                _orgCode = paras[0].OrgCode;
                PublicMethod.WriteContext(_userCode, _entCode, _orgCode, JsonConvert.SerializeObject(paras), this.GetType().Name);
                var items = ItemMaster.Finder.FindAll($"Code in ({PublicMethod.StringJoin(paras.Select(t => t.Code))}) and Org.Code = '{_orgCode}'").Cast<ItemMaster>().ToList();
                var notFounds = paras.Select(t => t.Code).Except(items.Select(t => t.Code)).ToList();
                if (notFounds.Count > 0)
                {
                    return new ApiResult<string>()
                    {
                        ResCode = 1,
                        ResMsg = $"料品{string.Join(",", notFounds)}未找到"
                    };
                }
                else
                {
                    using (ISession session = Session.Open())
                    {
                        foreach (var para in paras)
                        {
                            var item = items.FirstOrDefault(t => t.Code == para.Code);
                            item.Name = para.Name;
                            item.SPECS = para.SPECS;
                            item.ItemFormAttribute = ItemTypeAttributeEnum.GetFromValue((int)System.Enum.Parse(typeof(ParaItemAttributeEnum), para.ItemFormAttribute));
                            item.IsInventoryEnable = para.IsInventoryEnable ?? item.IsInventoryEnable;
                            item.IsPurchaseEnable = para.IsPurchaseEnable ?? item.IsPurchaseEnable;
                            item.IsSalesEnable = para.IsSalesEnable ?? item.IsSalesEnable;
                            item.IsBuildEnable = para.IsBuildEnable ?? item.IsBuildEnable;
                            item.IsOutsideOperationEnable = para.IsOutsideOperationEnable ?? item.IsOutsideOperationEnable;
                            item.IsBOMEnable = para.IsBOMEnable ?? item.IsBOMEnable;
                            item.IsVMIEnable = para.IsVMIEnable ?? item.IsVMIEnable;
                            if (!string.IsNullOrEmpty(para.MainItemCategory))
                                item.MainItemCategory = ItemCategory.Finder.Find($"Name = '{para.MainItemCategory}'") ?? throw new Exception($"未找到名称为 {para.MainItemCategory} 的料品分类");
                            if (!string.IsNullOrEmpty(para.UOM))
                            {
                                var uom = UOM.Finder.Find($"Name = '{para.Name}'") ?? throw new Exception($"未找到名称为 {para.UOM} 的计量单位");
                                item.InventoryUOM = uom;
                                item.InventorySecondUOM = uom;
                                item.SalesUOM = uom;
                                item.PurchaseUOM = uom;
                                item.PriceUOM = uom;
                                item.ManufactureUOM = uom;
                                item.MaterialOutUOM = uom;
                                item.CostUOM = uom;
                            }
                            item.Effective.IsEffective = true;
                        }
                        session.Commit();
                    }
                    return new ApiResult<string>();
                }
            }
        }

        public ApiResult<string> OAItemAdd()
        {
            var result = new ApiResult<string>();
            using (BPForEngine bp = new BPForEngine())
            {
                PublicMethod.WriteContext(_userCode, _entCode, _orgCode, JsonConvert.SerializeObject(""), this.GetType().Name);
                JsonConfig jsonConfig = new JsonConfig();
                OAItemAddPara oAItemAddPara = new OAItemAddPara();
                HttpHelper httpHelper = new HttpHelper();
                oAItemAddPara.header = httpHelper.OAPurMallToken();
                oAItemAddPara.data = new List<OAItemAddPara.Datum>()
                {
                    new OAItemAddPara.Datum
                    {
                        mainTable = new Maintable
                        {
                            Code = "0201.00001",
                            Name = "硫酸",
                            SPECS = "98%",
                            ItemFormAttribute = 4,
                            IsInventoryEnable = 0,
                            IsPurchaseEnable = 0,
                            IsSalesEnable = 0,
                            IsBuildEnable = 0,
                            IsOutsideOperationEnable = 0,
                            IsMRPEnable = 0,
                            IsBOMEnable = 0,
                            IsVMIEnable = 0,
                            IsEffective = 0,
                            MainItemCategory = "酸类",
                            UOM = "吨"
                        },
                        operationinfo = new Operationinfo
                        {
                            @operator="1"
                        }
                    }
                };
                httpHelper.URL = jsonConfig.SelectToken("OA.OAPurMallURL") + "/api/cube/restful/interface/saveOrUpdateModeData/cgsc";
                httpHelper.ContentType = "application/x-www-form-urlencoded";
                httpHelper.Content = $"datajson={WebUtility.UrlEncode(JsonConvert.SerializeObject(oAItemAddPara))}";
                result.Data = httpHelper.Request();
            }
            return result;
        }

        private List<SupplierDTOData> GetSupplierList(List<NewSupplierDTO> supplierDTOs, out List<SupplierDTOData> updateSuppliers)
        {
            updateSuppliers = new List<SupplierDTOData>();
            foreach (NewSupplierDTO newSupplierDTO in supplierDTOs)
            {
                SupplierDTOData supplierDTOData = new SupplierDTOData();
                //supplierDTOData.SupplierSiteDTOs.Add(new SupplierSiteDTOData() { Code = newSupplierDTO.Code });
                bool flag2 = newSupplierDTO.Category != null;
                if (flag2)
                {
                    supplierDTOData.Category = new CommonArchiveDataDTOData();
                    bool flag3 = !string.IsNullOrEmpty(newSupplierDTO.Category.Code);
                    if (flag3)
                    {
                        supplierDTOData.Category.Code = newSupplierDTO.Category.Code;
                    }
                }
                bool flag4 = !string.IsNullOrWhiteSpace(newSupplierDTO.Code);
                if (flag4)
                {
                    supplierDTOData.Code = newSupplierDTO.Code;
                }
                bool flag5 = newSupplierDTO.ContactObject != null;
                if (flag5)
                {
                    supplierDTOData.ContactObject = new CommonArchiveDataDTOData();
                    bool flag6 = newSupplierDTO.ContactObject.ID > 0L;
                    if (flag6)
                    {
                        supplierDTOData.ContactObject.ID = newSupplierDTO.ContactObject.ID;
                    }
                    bool flag7 = !string.IsNullOrWhiteSpace(newSupplierDTO.ContactObject.Code);
                    if (flag7)
                    {
                        supplierDTOData.ContactObject.Code = newSupplierDTO.ContactObject.Code;
                    }
                    bool flag8 = !string.IsNullOrWhiteSpace(newSupplierDTO.ContactObject.Name);
                    if (flag8)
                    {
                        supplierDTOData.ContactObject.Name = newSupplierDTO.ContactObject.Name;
                    }
                }
                supplierDTOData.QualityPledge = newSupplierDTO.QualityPledge;
                supplierDTOData.TradeCategory = newSupplierDTO.TradeCategory;
                supplierDTOData.State = newSupplierDTO.State;
                supplierDTOData.InvoiceVerificationOrder = newSupplierDTO.InvoiceVerificationOrder;
                supplierDTOData.InvoiceVerificationDetai = newSupplierDTO.InvoiceVerificationDetai;
                supplierDTOData.DocVerificationOrder = newSupplierDTO.DocVerificationOrder;
                supplierDTOData.EvaluateLevel = newSupplierDTO.EvaluateLevel;
                supplierDTOData.Bargain = newSupplierDTO.Bargain;
                bool flag9 = !string.IsNullOrEmpty(newSupplierDTO.PriceListCode);
                if (flag9)
                {
                    supplierDTOData.PriceListCode = newSupplierDTO.PriceListCode;
                }
                bool flag10 = newSupplierDTO.PriceListID > 0L;
                if (flag10)
                {
                    supplierDTOData.PriceListID = newSupplierDTO.PriceListID;
                }
                bool flag11 = !string.IsNullOrEmpty(newSupplierDTO.PriceListName);
                if (flag11)
                {
                    supplierDTOData.PriceListName = newSupplierDTO.PriceListName;
                }
                bool flag12 = !string.IsNullOrEmpty(newSupplierDTO.CorpUnifyCode);
                if (flag12)
                {
                    supplierDTOData.CorpUnifyCode = newSupplierDTO.CorpUnifyCode;
                }
                bool flag13 = newSupplierDTO.Country != null;
                if (flag13)
                {
                    supplierDTOData.Country = new CommonArchiveDataDTOData();
                    bool flag14 = newSupplierDTO.Country.ID > 0L;
                    if (flag14)
                    {
                        supplierDTOData.Country.ID = newSupplierDTO.Country.ID;
                    }
                    bool flag15 = !string.IsNullOrWhiteSpace(newSupplierDTO.Country.Code);
                    if (flag15)
                    {
                        supplierDTOData.Country.Code = newSupplierDTO.Country.Code;
                    }
                }
                bool flag16 = newSupplierDTO.Department != null;
                if (flag16)
                {
                    supplierDTOData.Department = new CommonArchiveDataDTOData();
                    bool flag17 = newSupplierDTO.Department.ID > 0L;
                    if (flag17)
                    {
                        supplierDTOData.Department.ID = newSupplierDTO.Department.ID;
                    }
                    bool flag18 = !string.IsNullOrWhiteSpace(newSupplierDTO.Department.Code);
                    if (flag18)
                    {
                        supplierDTOData.Department.Code = newSupplierDTO.Department.Code;
                    }
                }
                supplierDTOData.DescFlexField = newSupplierDTO.DescFlexField;
                supplierDTOData.Description = newSupplierDTO.Description;
                bool flag19 = newSupplierDTO.ID > 0L;
                if (flag19)
                {
                    supplierDTOData.ID = newSupplierDTO.ID;
                }
                supplierDTOData.IsMISC = newSupplierDTO.IsMISC;
                bool flag20 = !string.IsNullOrWhiteSpace(newSupplierDTO.ShortName);
                if (flag20)
                {
                    supplierDTOData.Name = newSupplierDTO.ShortName;
                }
                bool flag21 = newSupplierDTO.Org != null;
                if (flag21)
                {
                    supplierDTOData.Org = new CommonArchiveDataDTOData();
                    bool flag22 = newSupplierDTO.Org.ID > 0L;
                    if (flag22)
                    {
                        supplierDTOData.Org.ID = newSupplierDTO.Org.ID;
                    }
                    bool flag23 = !string.IsNullOrWhiteSpace(newSupplierDTO.Org.Code);
                    if (flag23)
                    {
                        supplierDTOData.Org.Code = newSupplierDTO.Org.Code;
                    }
                }
                else
                {
                    supplierDTOData.Org.Code = _orgCode;
                }
                bool flag24 = newSupplierDTO.Purchaser != null;
                if (flag24)
                {
                    supplierDTOData.Purchaser = new CommonArchiveDataDTOData();
                    bool flag25 = newSupplierDTO.Purchaser.ID > 0L;
                    if (flag25)
                    {
                        supplierDTOData.Purchaser.ID = newSupplierDTO.Purchaser.ID;
                    }
                    bool flag26 = !string.IsNullOrWhiteSpace(newSupplierDTO.Purchaser.Code);
                    if (flag26)
                    {
                        supplierDTOData.Purchaser.Code = newSupplierDTO.Purchaser.Code;
                    }
                }
                supplierDTOData.RegisterCapital = newSupplierDTO.RegisterCapital;
                bool flag27 = newSupplierDTO.RegisterCapitalCurrency != null;
                if (flag27)
                {
                    supplierDTOData.RegisterCapitalCurrency = new CommonArchiveDataDTOData();
                    bool flag28 = newSupplierDTO.RegisterCapitalCurrency.ID > 0L;
                    if (flag28)
                    {
                        supplierDTOData.RegisterCapitalCurrency.ID = newSupplierDTO.RegisterCapitalCurrency.ID;
                    }
                    bool flag29 = !string.IsNullOrEmpty(newSupplierDTO.RegisterCapitalCurrency.Code);
                    if (flag29)
                    {
                        supplierDTOData.RegisterCapitalCurrency.Code = newSupplierDTO.RegisterCapitalCurrency.Code;
                    }
                }
                bool flag30 = !string.IsNullOrWhiteSpace(newSupplierDTO.ShortName);
                if (flag30)
                {
                    supplierDTOData.ShortName = newSupplierDTO.ShortName;
                }
                bool flag31 = newSupplierDTO.SupplierBank != null;
                bool flag36 = newSupplierDTO.TaxSchedule != null;
                if (flag36)
                {
                    supplierDTOData.TaxSchedule = new CommonArchiveDataDTOData();
                    bool flag37 = newSupplierDTO.TaxSchedule.ID > 0L;
                    if (flag37)
                    {
                        supplierDTOData.TaxSchedule.ID = Convert.ToInt64(newSupplierDTO.TaxSchedule);
                    }
                    bool flag38 = !string.IsNullOrEmpty(newSupplierDTO.TaxSchedule.Code);
                    if (flag38)
                    {
                        supplierDTOData.TaxSchedule.Code = newSupplierDTO.TaxSchedule.Code;
                        //supplierDTOData.SupplierSiteDTOs[0].TaxSchedule = new CommonArchiveDataDTOData
                        //{
                        //    Code = newSupplierDTO.TaxSchedule.Code,
                        //};
                    }
                }
                bool flag39 = newSupplierDTO.Territory != null;
                if (flag39)
                {
                    supplierDTOData.Territory = new CommonArchiveDataDTOData();
                    bool flag40 = newSupplierDTO.Territory.ID > 0L;
                    if (flag40)
                    {
                        supplierDTOData.Territory.ID = newSupplierDTO.Territory.ID;
                    }
                    bool flag41 = !string.IsNullOrEmpty(newSupplierDTO.Territory.Code);
                    if (flag41)
                    {
                        supplierDTOData.Territory.Code = newSupplierDTO.Territory.Code;
                    }
                }
                bool flag42 = newSupplierDTO.TradeCurrency != null;
                if (flag42)
                {
                    supplierDTOData.TradeCurrency = new CommonArchiveDataDTOData();
                    bool flag43 = newSupplierDTO.TradeCurrency.ID > 0L;
                    if (flag43)
                    {
                        supplierDTOData.TradeCurrency.ID = newSupplierDTO.TradeCurrency.ID;
                        //supplierDTOData.SupplierSiteDTOs[0].TradeCurrency.ID = newSupplierDTO.TradeCurrency.ID;
                    }
                    if (flag39)
                    {
                        bool flag44 = !string.IsNullOrEmpty(newSupplierDTO.Territory.Code);
                        if (flag44)
                        {
                            supplierDTOData.Territory.Code = newSupplierDTO.Territory.Code;
                        }
                    }
                }
                //立账条件
                if (newSupplierDTO.APConfirmTerm != null && !string.IsNullOrEmpty(newSupplierDTO.APConfirmTerm.Code))
                {
                    supplierDTOData.APConfirmTerm = new CommonArchiveDataDTOData
                    {
                        Code = newSupplierDTO.APConfirmTerm.Code
                    };
                    //supplierDTOData.SupplierSiteDTOs[0].APConfirmTerm = new CommonArchiveDataDTOData
                    //{
                    //    Code = newSupplierDTO.APConfirmTerm.Code
                    //};
                }
                updateSuppliers.Add(supplierDTOData);
            }
            return updateSuppliers;
        }
    }
}
